1
จากชุดข้อมูลตัวอย่างสู่ความวุ่นวายในโลกจริง
EvoClass-AI002Lecture 5
00:00

1. สะพานเชื่อมช่องว่าง: พื้นฐานการโหลดข้อมูล

โมเดลเรียนรู้ลึก (Deep Learning) จะเติบโตได้ดีกับข้อมูลที่สะอาดและสม่ำเสมอ แต่ชุดข้อมูลในโลกจริงมักมีความยุ่งเหยิงโดยธรรมชาติ เราจำเป็นต้องเปลี่ยนจากแหล่งข้อมูลมาตรฐานที่จัดเตรียมไว้แล้ว (เช่น MNIST) มาสู่การจัดการกับแหล่งข้อมูลที่ไม่มีโครงสร้าง ซึ่งการโหลดข้อมูลเองกลายเป็นงานที่ซับซ้อน การวางรากฐานของกระบวนการนี้อยู่ที่เครื่องมือเฉพาะทางของ PyTorch สำหรับการจัดการข้อมูล

ปัญหาหลักคือการแปลงข้อมูลดิบและกระจายตัว (ภาพ ข้อความ ไฟล์เสียง) ที่เก็บอยู่บนดิสก์ให้กลายเป็นรูปแบบ รูปแบบเทนเซอร์ ที่ต้องการจากหน่วยประมวลผลกราฟิก (GPU) ซึ่งต้องใช้ตรรกะเฉพาะสำหรับการจัดทำดัชนี การโหลด การประมวลผลก่อน และในท้ายที่สุด คือการจัดกลุ่มข้อมูลเป็นชุดเล็กๆ

ความท้าทายสำคัญในข้อมูลจริง

  • ความยุ่งเหยิงของข้อมูล: ข้อมูลกระจัดกระจายอยู่ตามโฟลเดอร์หลายแห่ง โดยมักจะถูกดัชนีผ่านไฟล์ CSV เพียงอย่างเดียว
  • ต้องมีการประมวลผลก่อน: ภาพอาจต้องมีการปรับขนาด การปรับมาตรฐาน หรือเพิ่มข้อมูล (augmentation) ก่อนที่จะแปลงเป็นเทนเซอร์
  • เป้าหมายด้านประสิทธิภาพ: ข้อมูลต้องถูกส่งไปยัง GPU ในรูปแบบกลุ่มที่เหมาะสมและไม่หยุดชะงัก เพื่อเพิ่มความเร็วในการฝึกโมเดลให้สูงสุด
แนวทางแก้ไขของ PyTorch: การแยกหน้าที่ออกเป็นส่วนๆ
PyTorch กำหนดให้มีการแยกหน้าที่อย่างชัดเจน: คลาส Dataset จัดการกับ "อะไร" (วิธีเข้าถึงตัวอย่างและลาเบลเดี่ยวๆ) ส่วนที่ DataLoader จัดการกับ "วิธีการ" (การจัดกลุ่มอย่างมีประสิทธิภาพ การสับเปลี่ยนลำดับ และการส่งข้อมูลแบบหลายเธรด)
data_pipeline.py
TERMINALbash — data-env
> Ready. Click "Run" to execute.
>
TENSOR INSPECTOR Live

Run code to inspect active tensors
Question 1
What is the primary role of a PyTorch Dataset object?
To organize samples into mini-batches and shuffle them.
To define the logic for retrieving a single, preprocessed sample.
To perform the matrix multiplication inside the model.
Question 2
Which DataLoader parameter enables parallel loading of data using multiple CPU cores?
device_transfer
batch_size
num_workers
async_load
Question 3
If your raw images are all different sizes, which component is primarily responsible for resizing them to a uniform dimension (e.g., $224 \times 224$)?
The DataLoader's collate_fn.
The GPU's dedicated image processor.
The Transformation function applied within the Dataset's __getitem__ method.
Challenge: The Custom Image Loader Blueprint
Define the structure needed for real-world image classification.
You are building a CustomDataset for 10,000 images indexed by a single CSV file containing paths and labels.
Step 1
Which mandatory method must return the total number of samples?
Solution:
The __len__ method.
Concept: Defines the epoch size.
Step 2
What is the correct order of operations inside __getitem__(self, index)?
Solution:
1. Look up file path using index.
2. Load the raw data (e.g., Image).
3. Apply the necessary transforms.
4. Return the processed Tensor and Label.